package reflash;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import wumingya.com.studentsystem.ClassFragment;
import wumingya.com.studentsystem.MainActivity;
import wumingya.com.studentsystem.R;

import static android.content.ContentValues.TAG;
import static android.icu.text.DateTimePatternGenerator.PatternInfo.OK;

public class MyAdapter extends BaseAdapter {
	ArrayList<ApkEntity> apk_list;
	LayoutInflater inflater;
	private static final int OK = 2;//图片下载完成msg.what=1
	private static final int FAILE = -1;//图片下载失败msg.what=-1
	private static ImageView image;
	private File cache;
	private static List<ApkEntity> data;//数据
	@SuppressLint("HandlerLeak")
	public static Handler mHandler = new Handler()//数据UI的Handler
	{
		@SuppressWarnings("unchecked")
		public void handleMessage(android.os.Message msg) {
			if(msg.what == 1)
			{
				data.addAll((List<ApkEntity>) msg.obj);//数据下载完成则更新data
			}
			if(image!= null && msg.what == OK)
			{
				image.setImageURI((Uri) msg.obj);//图片下载完成则更新ImageView
			}
		};
	};
	public MyAdapter(Context context, ArrayList<ApkEntity> apk_list) {
		this.apk_list = apk_list;
		this.inflater = LayoutInflater.from(context);
	}

	public void onDateChange(ArrayList<ApkEntity> apk_list) {
		this.apk_list = apk_list;
		this.notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return apk_list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return apk_list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, final ViewGroup parent) {
		// TODO Auto-generated method stub
		ApkEntity entity = apk_list.get(position);
		final ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.item_layout, null);
			holder.name_tv = (TextView) convertView
					.findViewById(R.id.item3_apkname);
			holder.major_tv = (TextView) convertView
					.findViewById(R.id.item3_apkinfo);
			//holder.school_tv = (TextView) convertView
		//			.findViewById(R.id.item3_apkdes);
			holder.avatar_tv = (ImageView) convertView
					.findViewById(R.id.item3_apkiv);
			holder.viewBtn_tv = (Button) convertView
					.findViewById(R.id.item3_dlbtn);
			holder.id_tv = (TextView) convertView
					.findViewById(R.id.item3_apkid);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.name_tv.setText(entity.getName());
		holder.major_tv.setText(entity.getmajor_name());
		holder.id_tv.setText(entity.getId());
		//holder.school_tv.setText(entity.getschool_name());
		String img_url = "http://" + entity.getAvatar().toString();

		final String id = entity.getId();
		holder.viewBtn_tv.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				View parentView = (View) v.getParent();
				ClassFragment.setAnimation(parentView,parent.getContext(),position, id);
                Toast.makeText(parent.getContext(), "编号为:"+id+"的删除按钮被点击了", Toast.LENGTH_SHORT).show();
			}
		});
//		//判断获取的json中图片是否为空
//		if (TextUtils.isEmpty(data.get(position).getPic().toString())){
//			Picasso.with(con)
//					.cancelRequest(img);
//			img.setImageDrawable(con.getResources().getDrawable(R.mipmap.ic_launcher));//当图片为空时显示原始机器人图
//		}else {
//			//图片加载
//			Picasso
//					.with(con)
//					.load(list.get(position).getPic().toString())
//					.placeholder(R.mipmap.ic_launcher)//图片加载中时显示原始机器人图
//					.into(img);
//		}
		return convertView;
	}


	class ViewHolder {
		TextView id_tv;
		TextView name_tv;
		TextView major_tv;
		TextView school_tv;
		ImageView avatar_tv;
		Button viewBtn_tv;
	}


}
